﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunisoft.IrisSkin;
using System.IO;

namespace BossManager
{
    public partial class MainForm : Form
    {
        private bool MouseIsDown = false;
        private TextBox textListHoaDon=new TextBox();
        private SkinEngine SkinEng = new SkinEngine();
        private int X = 0;
        private int Y = 0;
        private int subItemSelected = 0;
        private string subItemText;
        private ListViewItem li;       
        public MainForm()
        {
            SkinEng.SerialNumber = "FON9tuWbgaq8lgc65NJ9WM9J8iVK/WMa7OzXIRe7Goh9O4CuFXhhTw==";
            SkinEng.SkinFile = Application.StartupPath + "\\GiaoDien\\MacOS11.ssk";
            InitializeComponent();
        }
        private void TaoMenu()
        {
            try
            {
                DirectoryInfo Dir = new DirectoryInfo(Application.StartupPath + "\\GiaoDien");
                FileInfo[] FileList = Dir.GetFiles("*.ssk", SearchOption.AllDirectories);
                foreach (FileInfo FI in FileList)
                {
                    ToolStripItem lx = new System.Windows.Forms.ToolStripMenuItem();
                    lx.Name = FI.Name;
                    lx.Text = FI.Name.Split('.')[0];
                    lx.Click += new EventHandler(lx_Click);
                    giaoDiệnToolStripMenuItem.DropDownItems.Add(lx);
                }
            }
            catch
            {

            }
        }
        void lx_Click(object sender, EventArgs e)
        {
            SkinEng.SkinFile = Application.StartupPath + "\\GiaoDien\\" + sender.ToString() + ".ssk";
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            textListHoaDon.Size = new System.Drawing.Size(0, 0);
            textListHoaDon.KeyPress += new KeyPressEventHandler(textListHoaDon_KeyPress);
            textListHoaDon.LostFocus += new EventHandler(textListHoaDon_LostFocus);            
            listHoaDon.Controls.AddRange(new Control[] { this.textListHoaDon });
            textListHoaDon.Hide();          
            TaoMenu();
        }     
      

       private void textListHoaDon_LostFocus(object sender, EventArgs e)
        {
            li.SubItems[subItemSelected].Text = textListHoaDon.Text;
            textListHoaDon.Hide();
        }

        void textListHoaDon_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                li.SubItems[subItemSelected].Text = listHoaDon.Text;
                textListHoaDon.Hide();
            }

            if (e.KeyChar == 27)
                textListHoaDon.Hide();
        }

        private void listHoaDon_DoubleClick(object sender, EventArgs e)
        {
            int nStart = X;
            int spos = 0;
            int epos =listHoaDon.Columns[0].Width;
            for (int i = 0; i < listHoaDon.Columns.Count; i++)
            {
                if (nStart > spos && nStart < epos)
                {
                    subItemSelected = i;
                    break;
                }

                spos = epos;
                epos += listHoaDon.Columns[i].Width;
            }
            subItemText = li.SubItems[subItemSelected].Text;
            string colName = listHoaDon.Columns[subItemSelected].Text;
            if (colName == "Số Lượng")
            {
                Rectangle r = new Rectangle(spos, li.Bounds.Y, epos, li.Bounds.Bottom);
                textListHoaDon.Size = new System.Drawing.Size(epos - spos, li.Bounds.Bottom - li.Bounds.Top);
                textListHoaDon.Location = new System.Drawing.Point(spos, li.Bounds.Y);
                textListHoaDon.Show();
                textListHoaDon.Text = subItemText;
                textListHoaDon.SelectAll();
                textListHoaDon.Focus();
            }

        }

        private void listHoaDon_MouseDown(object sender, MouseEventArgs e)
        {
            li =listHoaDon.GetItemAt(e.X, e.Y);
            X = e.X;
            Y = e.Y;
        }

        private void listHoaDon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                contextMenuHoaDon.Show(e.X, e.Y);
 
            }
        }

        private void listKhauPhan_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                contextMenuKhauPhan.Show(e.X, e.Y);

            }
        }

        private void Xemchitiet_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                contextXemCT.Show(e.X, e.Y);

            }
        }

        private void listBan_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                contextBan.Show(e.X, e.Y);

            }
        }

        private void listNo_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                contextMenuNo.Show(e.X, e.Y);
            }
        }

        private void listView2_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                contextMenuDatAn.Show(e.X, e.Y);
            }
        }

        private void hóaĐơnNợToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabNghiepVu.SelectedTab = tabOther;
        }

        private void listMonAn_MouseDown(object sender, MouseEventArgs e)
        {
            MouseIsDown = true;
        }

        private void listMonAn_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseIsDown)
            {
                listMonAn.DoDragDrop(listMonAn.FocusedItem.Text, DragDropEffects.Copy);
            }
            MouseIsDown = false;
        }

        private void listHoaDon_DragEnter(object sender, DragEventArgs e)
        {
            e.Data.GetData(DataFormats.Text);
            e.Effect = DragDropEffects.Copy;  
        }

        private void listHoaDon_DragDrop(object sender, DragEventArgs e)
        {
            if (!find(listHoaDon, e.Data.GetData(DataFormats.Text).ToString()))
            {
                    ListViewItem item = new ListViewItem();
                     item.Text = e.Data.GetData(DataFormats.Text).ToString();
                     item.SubItems.Add("1");
                     item.SubItems.Add("Tô");
                    item.SubItems.Add("200000");
                    listHoaDon.Items.Add(item);
            }
            else
            {                
                listHoaDon.Items[findID(listHoaDon, e.Data.GetData(DataFormats.Text).ToString())].SubItems[1].Text =
                    (int.Parse(listHoaDon.Items[findID(listHoaDon, e.Data.GetData(DataFormats.Text).ToString())].SubItems[1].Text) + 1).ToString();
                listHoaDon.Refresh();
            }
        }
        bool find(ListView lx, string s)
        {
            bool kt = false;
            foreach (ListViewItem du in lx.Items)
            {
                if (du.Text==s)
                    return true;
            }
            return kt;
        }
        int findID(ListView lx, string s)
        {
            for (int i = 0; i < lx.Items.Count; i++)
               if (lx.Items[i].Text == s)
                 return i;
            return -1;
            
            
        }
    }
}
